package com.itnetwork.commons;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XmlToHtml {
	/*
	 * xslSource xsl的源文件
	 * xmlSource xml的源文件
	 * htmlFile  生成html的file
	 */
	
	private static String[] regexs={"&lt;","&gt;"};
	private static String[] replacements={"<",">"};
	private static String tempHtml="temp.html";
	
	public static void write(StreamSource xslSource,StreamSource xmlSource,File htmlFile) throws FileNotFoundException, TransformerException 
	{
		  //创建一个类transformer的类厂
		TransformerFactory tFactory = TransformerFactory.newInstance();
		   //用该类厂创建一个transformer的实例
		Transformer transformer = tFactory.newTransformer(xslSource);
		   //调用transformer类的transform函数完成转换
		transformer.transform(xmlSource, new StreamResult(htmlFile));
		//打印出信息，告诉用户结果在sample.html文件中
		System.out.println("************* The result is in"+htmlFile.getPath()+" *************");
		XmlToHtml.changHtml(htmlFile);
		System.out.println("************* chang over!result in "+htmlFile.getPath()+"*************");
	}
	
	/*
	 *   targetFile 目标文件
	 *   此方法chang目标文件中的特殊字符
	 */
	public static void changHtml(File targetFile)
	{
		//StringBuffer sb=new StringBuffer(); 
		String allSt;
		File tempFile;
		String targetFilePath=targetFile.getPath();		//*news.html*\
		
		System.out.println(targetFilePath);
		String tempFilePath=targetFilePath.replaceAll(targetFile.getName(), tempHtml);
		tempFile =new File(tempFilePath);
		targetFile.renameTo(tempFile);//new.html重命名为temp.html
		
		System.out.println(targetFilePath);
		System.out.println(tempFilePath);
		
		//tempFile=new File(tempFilePath);
		
		 try {
			BufferedReader br=new BufferedReader(new FileReader(tempFile));
			BufferedWriter bw=new BufferedWriter(new FileWriter(targetFile));
			 String temp=null; 
			 temp=br.readLine();
	         while(temp!=null){ 
	        	 temp=XmlToHtml.replaceAll(regexs, replacements, temp);
	             //sb.append(temp+" "); 
	             bw.write(temp+"\n");
	             temp=br.readLine(); 
	         } 
	         br.close();
	         bw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		tempFile.delete();
	}
	/*
	 * 封装  被替换的特殊字符集与替换的字符集
	 */
	public static  String replaceAll(String[] regexs,String[] replacements,String temp)
	{
		if(regexs.length==replacements.length)
		{
			for(int i=0;i<regexs.length;i++)
			{
				temp=temp.replaceAll(regexs[i],replacements[i]);
			}
		}
		return temp;
	}
	
	/*public static void main(String args[])
	{
	
		File targetFile=new File("E:\\MyEclipse8.5\\itnetwork\\new.html");
		
			XmlToHtml.changHtml(targetFile);
		}*/
}
